{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_toqo18k"
   },
   "source": [
    "## Passing Values\n",
    "In the following example, the _value_ of `int i` is passed to the function `MultiplyByTwo`. Look carefully at the code and try to guess what the output will be before you execute it. When you are finished executing, click the button for an explanation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_k57ee3i",
    "graffitiConfig": {
     "executeCellViaGraffiti": "vs0iros_774bfxc"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using std::cout;\n",
    "\n",
    "\n",
    "int MultiplyByTwo(int i) {\n",
    "    i = 2*i;\n",
    "    return i;\n",
    "}\n",
    "\n",
    "int main() {\n",
    "    int a = 5;\n",
    "    cout << \"The int a equals: \" << a << \"\\n\";\n",
    "    int b = MultiplyByTwo(a);\n",
    "    cout << \"The int b equals: \" << b << \"\\n\";\n",
    "    cout << \"The int a still equals: \" << a << \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_vs0iros"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_vs0iros-id_774bfxc\"><i></i><button>Compile & Execute</button></span> &nbsp; <span class=\"graffiti-highlight graffiti-id_lihvne8-id_bxy0hmb\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_i0wc1j2",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_0v20ibz",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_i0wc1j2), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_kd6x7cr"
   },
   "source": [
    "In the code above, `a` is passed by value to the function, so the variable `a` is not affected by what happens inside the function.\n",
    "\n",
    "## Passing References\n",
    "\n",
    "But what if we wanted to change the value of `a` itself? For example, it might be that the variable you are passing into a function maintains some state in the program, and you want to write the function to update that state. \n",
    "\n",
    "It turns out, it is possible to modify `a` from within the function. To do this, you must pass a _reference_ to the variable `a`, instead of the _value_ of `a`. In C++, _a reference is just an alternative name for the same variable_.\n",
    "\n",
    "To pass by reference, you simply need to add an ampersand `&` before the variable in the function declaration. Try the code below to see how this works:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_o4vzxqz",
    "graffitiConfig": {
     "executeCellViaGraffiti": "uh4jq5n_xreh479"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using std::cout;\n",
    "\n",
    "\n",
    "int MultiplyByTwo(int &i) {\n",
    "    i = 2*i;\n",
    "    return i;\n",
    "}\n",
    "\n",
    "int main() {\n",
    "    int a = 5;\n",
    "    cout << \"The int a equals: \" << a << \"\\n\";\n",
    "    int b = MultiplyByTwo(a);\n",
    "    cout << \"The int b equals: \" << b << \"\\n\";\n",
    "    cout << \"The int a now equals: \" << a << \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_uh4jq5n"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_uh4jq5n-id_xreh479\"><i></i><button>Compile & Execute</button></span> &nbsp; <span class=\"graffiti-highlight graffiti-id_340gp27-id_witunwh\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_a91ke5k",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_ahn8i6y",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_a91ke5k), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_idwef9c"
   },
   "source": [
    "In the code above, `a` is passed by reference to the function `MultiplyByTwo` since the argument to `MultiplyByTwo` is a reference: `&i`. This means that `i` is becomes another name for whatever variable that is passed into the function. When the function changes the value of `i`, then the value of `a` is changed as well."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_hsdsg95"
   },
   "source": [
    "### Practice\n",
    "\n",
    "Modify the function below to accept a reference so that the passed variable can be directly modified by the function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_377nkxb",
    "graffitiConfig": {
     "executeCellViaGraffiti": "ldewkrl_c0pz3bd"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "#include <string>\n",
    "using std::cout;\n",
    "using std::string;\n",
    "\n",
    "\n",
    "void DoubleString(string value) {\n",
    "    // Concatentate the string with a space and itself.\n",
    "    value = value + \" \" + value;\n",
    "}\n",
    "\n",
    "int main() {\n",
    "    string s = \"Hello\";\n",
    "    cout << \"The string s is: \" << s << \"\\n\";\n",
    "    DoubleString(s);\n",
    "    cout << \"The string s is now: \" << s << \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_ldewkrl"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_ldewkrl-id_c0pz3bd\"><i></i><button>Compile & Execute</button></span> &nbsp; <span class=\"graffiti-highlight graffiti-id_bq8ouce-id_77f3cnv\"><i></i><button>See Solution</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_njq15mz",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_qobswjn",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_njq15mz), please wait...</i>"
   ]
  }
 ],
 "metadata": {
  "graffiti": {
   "firstAuthorId": "813558546",
   "id": "id_svft7kc",
   "language": "EN"
  },
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xeus-cling-cpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "-std=c++17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
